package com.example.yuanboot.controller.system;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yuanboot.common.anno.AnonymousAccess;
import com.example.yuanboot.common.enums.system.AccountStatusEnum;
import com.example.yuanboot.common.msg.BaseResultCode;
import com.example.yuanboot.common.msg.ObjectRestResponse;
import com.example.yuanboot.common.msg.ResponseUtils;
import com.example.yuanboot.common.msg.TableResponse;
import com.example.yuanboot.model.dto.system.SysUserDto;
import com.example.yuanboot.model.entity.system.SysRole;
import com.example.yuanboot.model.entity.system.SysUser;
import com.example.yuanboot.model.vo.page.PageInput;
import com.example.yuanboot.service.system.ISysRoleService;
import com.example.yuanboot.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/sys/user")
@RequiredArgsConstructor
public class SysUserController{

    private final ISysUserService sysUserService;

    @PostMapping("list")
    public TableResponse<SysUserDto> page(PageInput pageInput, String username) {
        IPage<SysUserDto> page = sysUserService.page(new Page<>(pageInput.getPage(), pageInput.getSize())
                , Wrappers.<SysUser>lambdaQuery().eq(StrUtil.isNotBlank(username), SysUser::getUsername, username))
                .convert(user -> {
                    SysUserDto dto = BeanUtil.copyProperties(user, SysUserDto.class);
                    dto.setStateInfo(AccountStatusEnum.getDesc(dto.getState()));
                    return dto;
                });
        return new TableResponse<>(page.getRecords(), page.getTotal());
    }
    @PostMapping("add")
    public ObjectRestResponse<SysUserDto> add(SysUserDto dto) {
        SysUser user = BeanUtil.copyProperties(dto, SysUser.class);
        sysUserService.save(user);
        return ResponseUtils.r(null, BaseResultCode.SUCCESS);
    }


}